cmake_minimum_required(VERSION 3.0.0)

# cmake-go-compiler framework

set(CMAKE_GO_COMPILER "go" CACHE STRING "Specify the Go compiler or path")

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(CMAKE_GO_FLAGS "-ldflags \"-s\"" CACHE STRING "Specify the Go compiler flags")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_GO_FLAGS "" CACHE STRING "Specify the Go compiler flags")
endif(CMAKE_BUILD_TYPE STREQUAL "Release")


set(CMAKE_GO_OUTPUT_SUFFIX "" CACHE STRING "Specifies the Go compile output file suffix")

function(check_suffix _SUFFIX)
    if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
        set(CMAKE_GO_OUTPUT_SUFFIX ".exe")
    else()
        set(CMAKE_GO_OUTPUT_SUFFIX "")
    endif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")

    set(_SUFFIX "${CMAKE_GO_OUTPUT_SUFFIX}" PARENT_SCOPE)
endfunction(check_suffix _SUFFIX)


# 构建一个 Go 程序, 可指定架构与后缀名称的
function(add_go_custom_executable NAME GOOS GOARCH SUFFIX GO_SOURCE_FILES)
    add_custom_target(${NAME} ALL 
        ${GOOS} ${GOARCH} 
            ${CMAKE_GO_COMPILER} build 
            -o ${CMAKE_CURRENT_BINARY_DIR}/${NAME}${SUFFIX}
            ${CMAKE_GO_FLAGS} ${GO_SOURCE_FILES}
        # SOURCES "${NAME}"
        BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${NAME}${SUFFIX}
        COMMENT "GoCompiler built: ${NAME}"
        WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
    )
endfunction(add_go_custom_executable NAME GOOS GOARCH SUFFIX GO_SOURCE_FILES)

# linux
function(add_go_linux_arm64_executable NAME)
    add_go_custom_executable(${NAME} "GOOS=linux" "GOARCH=arm64" "" "${ARGN}")
endfunction(add_go_linux_arm64_executable NAME)

function(add_go_linux_amd64_executable NAME)
    add_go_custom_executable(${NAME} "GOOS=linux" "GOARCH=amd64" "" "${ARGN}")
endfunction(add_go_linux_amd64_executable NAME)

# linux mips
function(add_go_linux_mips_hf_executable NAME)
    add_go_custom_executable(${NAME} "GOOS=linux" "GOARCH=mips" "" "${ARGN}")
endfunction(add_go_linux_mips_hf_executable NAME)

function(add_go_linux_mips_sf_executable NAME)
    add_go_custom_executable(${NAME} "GOOS=linux" "GOARCH=mips GOMIPS=softfloat" "" "${ARGN}")
endfunction(add_go_linux_mips_sf_executable NAME)

# windows
function(add_go_win_arm64_executable NAME)
    add_go_custom_executable(${NAME} "GOOS=windows" "GOARCH=arm64" ".exe" "${ARGN}")
    # set_target_properties(${NAME} PROPERTIES SUFFIX ".exe")
endfunction(add_go_win_arm64_executable NAME)

function(add_go_win_amd64_executable NAME)
    add_go_custom_executable(${NAME} "GOOS=windows" "GOARCH=amd64" ".exe" "${ARGN}")
endfunction(add_go_win_amd64_executable NAME)

# macOS(darwin)
function(add_go_darwin_arm64_executable NAME)
    add_go_custom_executable(${NAME} "GOOS=darwin" "GOARCH=arm64" "" "${ARGN}")
endfunction(add_go_darwin_arm64_executable NAME)

function(add_go_darwin_amd64_executable NAME)
    add_go_custom_executable(${NAME} "GOOS=darwin" "GOARCH=amd64" "" "${ARGN}")
endfunction(add_go_darwin_amd64_executable NAME)


# 构建一个 Go 程序
function(add_go_executable NAME)
    add_go_custom_executable(${NAME} "" "" "" "${ARGN}")
endfunction(add_go_executable NAME)


# TODO ######################################################
# 准备一次性解决 构建问题并制作为相对于涉及到 CMake 的标准构建动作

# 如果你的项目没有 go.mod 将需要添加该部分
# 例如: 
#     # go get github.com/godbus/dbus
#     add_golang_module(
#         "github.com/godbus/dbus"
#     )

# 如果需要在 configure 的时候进行构建操作
#   直接使用 execute_process()
# 如果是需要在 make 的时候才进行构建操作
#    add_golang_executable(${MODULE_NAME})

# 如果要构建目标 输出到源代码目录
# 如果要构建目标 输出到相对输出目录
# 如果要构建目标 由指定源代码文件
# 如果要构建目标 由任意macro操作(应该是不可能的)

#############################################################

# Features: 

# 此文件提供以下几种目标构建,
# add_go_linux_arm64_executable(<TARGET> [SOURCES ...])
# add_go_linux_amd64_executable(<TARGET> [SOURCES ...])
# add_go_linux_mips_hf_executable(<TARGET> [SOURCES ...])
# add_go_linux_mips_sf_executable(<TARGET> [SOURCES ...])
# add_go_win_arm64_executable(<TARGET> [SOURCES ...])
# add_go_win_amd64_executable(<TARGET> [SOURCES ...])
# add_go_darwin_arm64_executable(<TARGET> [SOURCES ...])
# add_go_darwin_amd64_executable(<TARGET> [SOURCES ...])
# add_go_executable(<TARGET> [SOURCES ...])

# USE 
# - 拥有对于未包含 go.mod 的编译支持 
# set(CMAKE_GO_COMPILER "go")
# set(CMAKE_GO_FLAGS "")

# find_package(GoCompiler PATHS ${CMAKE_SOURCE_DIR})

# add_go_executable(${PROJECT_NAME} main.go)